﻿DECLARE @Var_CallNo      INT          SET @Var_CallNo      = (@CallNo_In)     --不同产商的唯一号     
DECLARE @Var_CallName    VARCHAR(100) SET @Var_CallName    = (@CallName_In)   --操作员名称  
                                      
--DECLARE @Var_CardType    INT          SET @Var_CardType    = (@CardType_In)   --卡类型   
DECLARE @Var_CardNo      VARCHAR(100) SET @Var_CardNo      = (@CardNo_In)     --卡号      
DECLARE @Var_PatientName VARCHAR(100) SET @Var_PatientName = (@PatientName_In)--病人姓名
DECLARE @Var_IdCardNo    VARCHAR(100) SET @Var_IdCardNo    = (@IdCardNo_In)   --身份证号   
DECLARE @Var_Sex         INT          SET @Var_Sex         = (@Sex_In)       --性别        
DECLARE @Var_Birthday    DATETIME     SET @Var_Birthday    = (@Birthday_In)   --生日   
DECLARE @Var_Address     VARCHAR(256) SET @Var_Address     = (@Address_In)    --地址    

DECLARE @HIS_PatientId   VARCHAR(100)
DECLARE @HIS_PatientName VARCHAR(100)
DECLARE @HIS_Tel         VARCHAR(100)
DECLARE @HIS_Birthday    DATETIME
DECLARE @HIS_Sex         INT
DECLARE @HIS_CreateDate  DATETIME
DECLARE @HIS_PersonId    VARCHAR(100)

DECLARE @HIS_BindingDate DATETIME

SET @HIS_BindingDate=GETDATE()

SELECT TOP 1 @HIS_PatientId = PATIENTID
            ,@HIS_PatientName = PATIENTNAME
            ,@HIS_Birthday = CAST(BIRTHDAY AS DATE)
            ,@HIS_Sex = CASE WHEN SEX = '男' THEN 1 WHEN SEX = '女' THEN 0 ELSE 3 END
            ,@HIS_Tel = Tel
            ,@HIS_CreateDate = CreateDate
			,@HIS_PersonId = PersonID
        FROM PATIENTBASE (NOLOCK) 
       WHERE PERSONID = @CardNo_In 
    ORDER BY BINDINGDATE DESC,OperDate DESC

IF @HIS_PatientId IS NOT NULL
BEGIN
    IF @Var_PatientName = @HIS_PatientName AND @HIS_Birthday = CAST(@Var_Birthday AS DATE) AND @Var_Sex = @HIS_Sex
    BEGIN
		SELECT 1 AS Result,'成功' AS ResultText,
               @HIS_PatientId AS PatientId,
			   @HIS_PatientName AS PatientName,
			   @HIS_PersonId AS IdCardNo,
			   @HIS_Sex AS Sex,
			   @HIS_Birthday AS Birthday,
               @HIS_Tel AS Tel,
               @HIS_CreateDate AS CreateDate
      RETURN
    END ELSE
    BEGIN
      SELECT -1 AS RESULT,'已经存在该病人的基本信息，但与此次登录的信息不匹配，请联系人工窗口或系统维护员！' AS RESULTTEXT
      RETURN
    END
END ELSE
BEGIN
    EXEC GETUNIQUENO 26,@NEWUNIQUENO=@HIS_PatientId OUTPUT
    INSERT INTO PATIENTBASE
				(PATIENTID
				,PATIENTNAME
				,PYM
				,SEX
				,BIRTHDAY
				,ADDRESS
				,TEL
				,PERSONID
				,OPERNAME
				,OPERDATE
				,CREATEDATE
				,CREATEOPER
				,BINDINGDATE
				,BINDINGOPERNAME
                ,NationType
        )
			VALUES
				(@HIS_PatientId
				,@Var_PatientName
				,'AP'
				,CASE WHEN (@Var_Sex)=1 THEN '男' WHEN (@Var_Sex)=2 THEN '女' END
				,@Var_BirthDay
				,@Var_Address
				,null
				,@Var_IdCardNo
				,@Var_CallName
				,@HIS_BindingDate
				,@HIS_BindingDate
				,@Var_CallName
				,null
				,@Var_CallName
                ,null
				)

      SELECT 1 AS Result,'成功' AS ResultText,
             PATIENTID AS PatientId,
             TEL AS Tel,
             CREATEDATE AS CreateDate
      FROM dbo.PATIENTBASE 
      WHERE @@ROWCOUNT > 0 AND PatientId = @HIS_PatientId
END